import 'dart:convert';

import 'package:qunaimedical/models/user.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SPUtils {
  SPUtils._internal();

  static SharedPreferences _spf;

  static Future<SharedPreferences> init() async {
    if (_spf == null) {
      _spf = await SharedPreferences.getInstance();
    }
    return _spf;
  }

  /// 语言
  static Future<bool> setLanguageCode(String languageCode) {
    return _spf.setString('language_code', languageCode);
  }

  static String getLanguageCode() {
    return _spf.getString('language_code');
  }

  /// 首次引导
  static Future<bool> setFirst(bool first) {
    return _spf.setBool('first', first);
  }

  static bool isFirst() {
    return _spf.getBool('first') ?? true;
  }

  static Future<bool> setUser(User value) {
    if (value == null) {
      return _spf.remove('user');
    }
    return _spf.setString('user', json.encode(value.toMap()));
  }

  static User getUser() {
    String value = _spf.getString('user');
    if (value == null) {
      return null;
    } else {
      return User.fromJson(json.decode(value));
    }
  }
}
